<?php
class Application_Model_Acl extends Application_Model_Db_Db
{
    /**
     * get all project resources
     * @return array
     */
    public function getAllControllers() {
        $sql = "SELECT 
                    `id`,
                    `resource`
                FROM `resource`
                WHERE `parent_id` = 0";
        return $this->_db->fetchAll($sql);
    }

    /**
     * get all project roles
     * @return array
     */
    public function getAllRoles() {
        $sql = "SELECT                  
                    `id`,
                    `role`
                FROM `role`";
        return $this->_db->fetchAll($sql);
    }

    /**
     * 
     * @param $roleId
     * @param int $controllerId
     * @return array
     */
    public function getRoleResources($roleId, $controllerId=0) {
        $sql = "SELECT
                    `resource`.`id`,
                    `resource`.`resource`
                FROM `role_privileges` AS `privileges`
                INNER JOIN `resource`
                    ON `privileges`.`resource_id` = `resource`.`id`
                WHERE 
                    1
                    AND `privileges`.`role_id` = ?
                    AND `resource`.`parent_id` = ?";
        return $this->_db->fetchAll($sql, array($roleId, $controllerId));
    } 

    public function getAllRoleResources($roleId) {
        $sql = "SELECT
                    `resource`.`id`,
                    `resource`.`resource`,
                    IFNULL(`parent`.`resource`, '') AS `parent_resource`
                FROM `role_privileges` AS `privileges`
                INNER JOIN `resource`
                    ON `privileges`.`resource_id` = `resource`.`id`                                
                LEFT JOIN `resource` AS `parent`
                    ON `resource`.`parent_id` = `parent`.`id`    
                WHERE 
                    1
                    AND `privileges`.`role_id` = ?";
        return $this->_db->fetchAll($sql, $roleId);
    } 

    /**
     * Return available for current user categories id's
     * in string format like "12,2,43,42,5"
     * @param int $roleId
     * @return string
     */
    public function getRoleAvailableCategoriesId($roleId) {        
        $sql = "SELECT
                    `category_id`
                FROM `role_categories`
                WHERE `role_id` = ?";        
        $arrayCategories = $this->_db->fetchAll($sql, $roleId);        
        $stringCategories = '';
        $isFirst = true;
        foreach ($arrayCategories as $category) {
            if ($isFirst) {
                $stringCategories .= $category['category_id'];
                $isFirst = false;
            } else {
                $stringCategories .= ',' . $category['category_id'];
            }
        }
        return $stringCategories;
    }

    public function getAllResources() {
        $sql = "SELECT
                   `resource`.`id`,
                   `resource`.`resource`,
                   IFNULL(`parent`.`resource`, '') AS `parent_resource`
                FROM `resource`
                LEFT JOIN `resource` AS `parent`
                    ON `resource`.`parent_id` = `parent`.`id`                
                ORDER BY `resource`.`resource`";
        return $this->_db->fetchAll($sql);
    }

    public function getAllResourcesExcept($roleResources) {
        $where = '';
        if ($roleResources) {
            $where = " WHERE `resource`.`id` NOT IN ($roleResources)";
        }
        $sql = "SELECT
                   `resource`.`id`,
                   `resource`.`resource`,
                   IFNULL(`parent`.`resource`, '') AS `parent_resource`
                FROM `resource`
                LEFT JOIN `resource` AS `parent`
                    ON `resource`.`parent_id` = `parent`.`id`                
                {$where}
                ORDER BY `resource`.`resource`";        
        return $this->_db->fetchAll($sql);
    }
}